//
//  THTitleView.m
//  duolaCard
//
//  Created by 希达 on 2018/5/11.
//  Copyright © 2018年 zyy. All rights reserved.
//

#import "THTitleView.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import "UIView+Layout.h"

@interface THTitleView ()
@property (weak, nonatomic) UILabel *titleLbl;
@property (weak, nonatomic) UIButton *doneBtn;
@end

@implementation THTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configView];
    }
    return self;
}

- (void)configView {
    CGFloat top = TZCommonTools.tz_isIPhoneX ? 44 : 20;
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(100, top, self.frame.size.width - 200, 44)];
    titleLbl.textColor = [UIColor blackColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    titleLbl.font = [UIFont systemFontOfSize:18];
    [self addSubview:titleLbl];
    self.titleLbl = titleLbl;

    UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - 100, top, 100, 44)];
    doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:doneBtn];
    self.doneBtn = doneBtn;

}

- (void)doneBtnClick:(UIButton *)sender {
    if (self.th_titleViewDoneClickBlock) {
        self.th_titleViewDoneClickBlock();
    }
}

- (void)setIsShowDone:(BOOL)isShowDone {
    _isShowDone = isShowDone;
    self.doneBtn.hidden = isShowDone;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [self cubeAnimate:self.titleLbl flightInfo:title];
}

- (void)cubeAnimate:(UILabel *)targetView flightInfo:(NSString *)flightInfo {
    // 复制UIView，作为底面
    UILabel *viewCopy = [[UILabel alloc] initWithFrame:targetView.frame];
    viewCopy.alpha = 0;
    viewCopy.text = flightInfo;
    viewCopy.font = targetView.font;
    viewCopy.textAlignment = targetView.textAlignment;
    viewCopy.textColor = targetView.textColor;
    viewCopy.backgroundColor = [UIColor clearColor];
    // 设置底面UIView的初始位置和高度
    viewCopy.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1.0, 0.1), CGAffineTransformMakeTranslation(1.0, viewCopy.tz_height / 2));
    [self addSubview: viewCopy];
    [UIView animateWithDuration:0.3 animations:^{
        // 执行UIView和UIViewCopy的动画
        targetView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1.0, 0.1), CGAffineTransformMakeTranslation(1.0, -targetView.tz_height / 2));
        targetView.alpha = 0;
        viewCopy.alpha = 1;
        viewCopy.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        // 当动画执行完毕后，将UIViewCopy的信息赋值给UIView，并还原UIView的状态，即与UIViewCopy相同的状态，然后移除UIViewCopy
        targetView.alpha = 1;
        targetView.text = viewCopy.text;
        targetView.transform = CGAffineTransformIdentity;
        [viewCopy removeFromSuperview];
    }];
}


@end



